package com.eastchina.fids2focs.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.eastchina.fids2focs.common.FIDSIIError;
import com.eastchina.fids2focs.common.exception.FIDSIIValidateException;
import com.eastchina.fids2focs.domain.SystemRole;
import com.eastchina.fids2focs.domain.SystemRoleAgent;
import com.eastchina.fids2focs.domain.SystemRoleAirport;
import com.eastchina.fids2focs.domain.SystemRoleEvent;
import com.eastchina.fids2focs.domain.SystemRolePriv;
import com.eastchina.fids2focs.domain.SystemRoleVip;
import com.eastchina.fw.database.dao.QueryDAO;
import com.eastchina.fw.database.dao.SqlHolder;
import com.eastchina.fw.database.dao.UpdateDAO;

/**
 * <p>
 * [系统管理].[用户管理].[角色维护]
 * </p> 
 */
@WebService(serviceName="RoleService", 
		endpointInterface="com.eastchina.fids2focs.service.RoleService")
public class RoleServiceImpl implements RoleService {

	/**
	 * 查询DAO
	 */
	@Autowired
	QueryDAO queryDAO;
	
	/**
	 * 更新DAO
	 */
	@Autowired
	UpdateDAO updateDAO;
	
	/**
	 * <p> 增加角色信息 </p>
	 * @param role 角色信息  
	 */
	public void insert(SystemRole role) throws FIDSIIValidateException {
		if (role == null || StringUtils.isEmpty(role.getRoleId())) {
			return;
		}

		// Check if the role has existed
		SystemRole dbRole = queryDAO.executeForObject("SYSTEM_ROLE.select", role.getRoleId(), SystemRole.class);
		if (dbRole != null) {
			// Already exists
			throw new FIDSIIValidateException("Validate Error. Check you input values.", new FIDSIIError("A1101W.E00001"));
		}
		
		// Create SQLHolders
		List<SqlHolder> sqlHolders = new ArrayList<SqlHolder>();
		// Insert into SYSTEM_ROLE		
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE.insert", role));		
		// Insert into SYSTEM_ROLE_AGENT
		if (role.getSystemRoleAgents() != null && !role.getSystemRoleAgents().isEmpty()) {
			for (SystemRoleAgent roleAgent : role.getSystemRoleAgents()) {
				sqlHolders.add(new SqlHolder("SYSTEM_ROLE_AGENT.insert", roleAgent));
			}
		}
		// Insert into SYSTEM_ROLE_AIRPORT
		if (role.getSystemRoleAirports() != null && !role.getSystemRoleAirports().isEmpty()) {
			for (SystemRoleAirport roleAirport : role.getSystemRoleAirports()) {
				sqlHolders.add(new SqlHolder("SYSTEM_ROLE_AIRPORT.insert", roleAirport));
			}
		}
		// Insert into SYSTEM_ROLE_EVENT
		if (role.getSystemRoleEvents() != null && !role.getSystemRoleEvents().isEmpty()) {
			for (SystemRoleEvent roleEvent : role.getSystemRoleEvents()) {
				sqlHolders.add(new SqlHolder("SYSTEM_ROLE_EVENT.insert", roleEvent));
			}
		}
		// Insert into SYSTEM_ROLE_PRIV
		if (role.getSystemRolePrivs() != null && !role.getSystemRolePrivs().isEmpty()) {
			for (SystemRolePriv rolePriv : role.getSystemRolePrivs()) {
				sqlHolders.add(new SqlHolder("SYSTEM_ROLE_PRIV.insert", rolePriv));
			}
		}
		// Insert into SYSTEM_ROLE_VIP
		if (role.getSystemRoleVips() != null && !role.getSystemRoleVips().isEmpty()) {
			for (SystemRoleVip roleVip : role.getSystemRoleVips()) {
				sqlHolders.add(new SqlHolder("SYSTEM_ROLE_VIP.insert", roleVip));
			}
		}

		// Execute
		updateDAO.executeBatch(sqlHolders);
	}
	
	/**
	 * <p> 删除指定角色ID的角色信息 (级联) </p>
	 * @param roleId 角色ID
	 */
	public void delete(String roleId) {
		
		// Create SQLHolders
		List<SqlHolder> sqlHolders = new ArrayList<SqlHolder>();
		// Delete role information from SYSTEM_ROLE
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE.delete", roleId));		
		// SYSTEM_ROLE_AGENT
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE_AGENT.deleteByRoleId", roleId));
		// SYSTEM_ROLE_AIRPORT
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE_AIRPORT.deleteByRoleId", roleId));
		// SYSTEM_ROLE_EVENT
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE_EVENT.deleteByRoleId", roleId));
		// SYSTEM_ROLE_PRIV
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE_PRIV.deleteByRoleId", roleId));
		// SYSTEM_ROLE_VIP
		sqlHolders.add(new SqlHolder("SYSTEM_ROLE_VIP.deleteByRoleId", roleId));
		
		// Execute the SQL
		updateDAO.executeBatch(sqlHolders);
		
	}
	
	/**
	 * <p> 更新指定角色ID的角色信息 (级联) </p>
	 * @param role 角色信息
	 */
	public void update(SystemRole role) throws FIDSIIValidateException {
		// Delete the role information
		delete(role.getRoleId());
		
		// Insert the role information
		insert(role);
	}
	
	/**
	 * <p> 查询指定角色ID的角色信息</p>
	 * @param role 角色ID
	 */
	public SystemRole select(String roleId) {
		return select(roleId, true);
	}
	
	/**
	 * <p> 查询指定角色ID的角色信息</p>
	 * @param role 角色ID
	 * @param cascade 
	 */
	public SystemRole select(String roleId, boolean cascade) {
		if (!cascade) {
			return queryDAO.executeForObject("SYSTEM_ROLE.select", roleId, SystemRole.class);
		} else {
			return queryDAO.executeForObject("SYSTEM_ROLE.selectCascade", roleId, SystemRole.class);	
		}
	}
	
	/**
	 * <p> 查询所有角色的角色信息</p>
	 */
	public List<SystemRole> selectAllRoles() {
		return selectAllRoles(true);
	}

	/**
	 * <p> 查询所有角色的角色信息</p>
	 * @param cascade
	 */
	private List<SystemRole> selectAllRoles(boolean cascade) {
		if (!cascade) {
			return queryDAO.executeForObjectList("SYSTEM_ROLE.selectAllRoles", QueryDAO.EMPTY_PARAMS);
		} else {
			// Select All Role
			return queryDAO.executeForObjectList("SYSTEM_ROLE.selectAllRolesCascade", QueryDAO.EMPTY_PARAMS);
		}
	}
}
